import axios from 'axios';

// 通常做法

// 第一步 & 第二步
export class Api {

  constructor (api) {
    this.api = api;
  }

  static URL = {
    A: '/api/a',
    B: '/api/b',
    C: '/api/c',
  };

  getRes (params) {
    return axios({
      method: 'get',
      url: Api.URL[this.api],
      params,
    });
  }

  getMutationFunc () {
    return function (state, newValue) {
      state['value' + this.api] = newValue;
    };
  }

  getActionFunc () {
    return async function (ctx, params) {
      const res = await this.getRes(params);
      ctx.commit('SET_DATA_' + this.api, res);
    };
  }

}

// 第三步
const a = new Api('A');
const b = new Api('B');
const c = new Api('C');

export default {
  state: {
    valueA: [],
    valueB: [],
    valueC: [],
  },
  mutations: {
    SET_DATA_A: a.getMutationFunc(),
    SET_DATA_B: b.getMutationFunc(),
    SET_DATA_C: c.getMutationFunc(),
  },
  actions: {
    updateDataA: a.getActionFunc(),
    updateDataB: b.getActionFunc(),
    updateDataC: c.getActionFunc(),
  },
}
